package com.hopedove.yieldmanage.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.hopedove.utils.ParseUtils;
import com.hopedove.view.SimpleDialog;
import com.hopedove.yieldmanage.R;
import com.hopedove.yieldmanage.entity.PiBuModel;
import com.hopedove.yieldmanage.net.IServiceImpl;
import com.hopedove.yieldmanage.net.RequestServiceImpl;
import com.hopedove.yieldmanage.utils.SharedPreUtil;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class BangQueryActivity extends MIP_BaseActivity {
    RecyclerView recylistview;
    ImageView nodateImg;
    List<PiBuModel> piBuModelList = new ArrayList<PiBuModel>();
    QuickAdapter  quickAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bang_query);
        recylistview=(RecyclerView)this.findViewById(R.id.recylistview);
        nodateImg=(ImageView)this.findViewById(R.id.nodateImg);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recylistview.setLayoutManager(layoutManager);
        //给RecyclerView设置适配器
        quickAdapter = new QuickAdapter();
        recylistview.setAdapter(quickAdapter);
        recylistview.setHasFixedSize(true);
        recylistview.setNestedScrollingEnabled(false);
        showWait();
        String userBean = (String) SharedPreUtil.getValue(this, SharedPreUtil.USERBEAN, "");
        RequestServiceImpl.getBanList(mMIPHandler, IServiceImpl.BANG_QUERY,userBean);
    }
    public class QuickAdapter extends BaseQuickAdapter<PiBuModel, BaseViewHolder> {
        public QuickAdapter() {
            super(R.layout.item_pibu, piBuModelList);
        }

        @Override
        protected BaseViewHolder createBaseViewHolder(View view) {
            return super.createBaseViewHolder(view);
        }

        @Override
        protected void convert(BaseViewHolder holder, final PiBuModel item) {
            TextView item_leixing = holder.getView(R.id.item_leixing);
            TextView item_buhaoone = holder.getView(R.id.item_buhaoone);
            TextView item_buhaotwo = holder.getView(R.id.item_buhaotwo);
            TextView item_sanlianhao = holder.getView(R.id.item_sanlianhao);
            TextView item_chicun = holder.getView(R.id.item_chicun);
            TextView item_bupiaozhong = holder.getView(R.id.item_bupiaozhong);
            TextView item_chenzhong= holder.getView(R.id.item_chenzhong);
            TextView  item_yuantong= holder.getView(R.id.item_yuantong);
            TextView  item_beizhu= holder.getView(R.id.item_beizhu);
            TextView  item_michang= holder.getView(R.id.item_michang);
            TextView  item_bangci= holder.getView(R.id.item_bangci);
            TextView  item_chehao= holder.getView(R.id.item_chehao);
            TextView  item_leibie= holder.getView(R.id.item_leibie);
            TextView  item_heyuehao= holder.getView(R.id.item_heyuehao);
            TextView  item_gonghaoone= holder.getView(R.id.item_gonghaoone);
            TextView  item_geshuone= holder.getView(R.id.item_geshuone);
            TextView  item_gonghaotwo= holder.getView(R.id.item_gonghaotwo);
            TextView  item_geshutwo= holder.getView(R.id.item_geshutwo);
            TextView  item_gonghaothree= holder.getView(R.id.item_gonghaothree);
            TextView  item_geshuthree= holder.getView(R.id.item_geshuthree);
            TextView  item_chenzhongtime= holder.getView(R.id.item_chenzhongtime);

            item_leixing.setText(item.getLX() == null ? "" : item.getLX());
            item_buhaoone.setText(item.getCLOTHNO1()== null ? "" : item.getCLOTHNO1());
            item_buhaotwo.setText(item.getCLOTHNO2()== null ? "" : item.getCLOTHNO2());
            item_sanlianhao.setText(item.getCLOTHTKTNO()== null ? "" : item.getCLOTHTKTNO());
            item_chicun.setText(item.getCLOTHSIZE()== null ? "" : item.getCLOTHSIZE());
            item_bupiaozhong.setText(item.getCLOTHWEIGHT()== null ? "" : item.getCLOTHWEIGHT());
            item_chenzhong.setText(item.getFINISHWEIGHT()== null ? "" : item.getFINISHWEIGHT());
            item_yuantong.setText(item.getCYTUBE()==null?"":item.getCYTUBE());
            item_beizhu.setText(item.getREMARK()==null?"":item.getREMARK());
            item_michang.setText(item.getMETERLENGTH()==null?"":item.getMETERLENGTH());
            item_bangci.setText(item.getPROCLASS()==null?"":item.getPROCLASS());
            item_chehao.setText(item.getMACHINENO()==null?"":item.getMACHINENO());
            item_leibie.setText(item.getCLOTHTYPE()==null?"":item.getCLOTHTYPE());
            item_heyuehao.setText(item.getCONTACTNO()==null?"":item.getCONTACTNO());
            item_gonghaoone.setText(item.getEMPNO1()==null?"":item.getEMPNO1());
            item_geshuone.setText(item.getCELLNO1()==null?"":item.getCELLNO1());
            item_gonghaotwo.setText(item.getEMPNO2()==null?"":item.getEMPNO2());
            item_geshutwo.setText(item.getCELLNO2()==null?"":item.getCELLNO2());
            item_gonghaothree.setText(item.getEMPNO3()==null?"":item.getEMPNO3());
            item_geshuthree.setText(item.getCELLNO3()==null?"":item.getCELLNO3());
            item_chenzhongtime.setText(item.getWEIGHDATE()==null?"":item.getWEIGHDATE());
        }

    }
    @Override
    public void onPostHandle(int requestType, Object objBody, boolean error) throws Exception {
        stopWait();
        String msg = "";
        String opt = "";
        if (requestType == IServiceImpl.BANG_QUERY) {
            try {
                JSONObject jsonObj = new JSONObject((String) objBody);
                msg = jsonObj.optString("MSG_INFO");
                opt = jsonObj.optString("OPT_FLAG");
                if ("SUCCESS".equals(opt)) {
                    JSONArray dataarray = jsonObj.optJSONArray("DATA");
                    if (dataarray != null&&dataarray.length()>0) {
                        piBuModelList.clear();
                        for (int i = 0; i < dataarray.length(); i++) {
                            PiBuModel piBuModel = ParseUtils.parse(dataarray.getJSONObject(i).toString(), PiBuModel.class);
                            piBuModelList.add(piBuModel);
                        }

                    } else {
                        piBuModelList.clear();
                    }
                    quickAdapter.notifyDataSetChanged();
                    checkListview();
                } else {
                    if (TextUtils.isEmpty(msg)) {
                        SimpleDialog.show(this, "出错");
                    } else {
                        SimpleDialog.show(this, msg);
                    }
                    checkListview();
                }

            } catch (Exception e) {
                e.printStackTrace();
                SimpleDialog.show(this, "出错");
            }
        }
    }
    /**
     * 检查listview是否为空
     */
    private void checkListview() {
        if (piBuModelList.size() > 0) {
            recylistview.setVisibility(View.VISIBLE);
            nodateImg.setVisibility(View.GONE);
        } else {
            recylistview.setVisibility(View.GONE);
            nodateImg.setVisibility(View.VISIBLE);
        }
    }

}
